package cn.z2huo.usercenter.authentication.app.aspect.tokenbucket;

import cn.z2huo.usercenter.util.RedisKeyGenerateUtils;
import lombok.RequiredArgsConstructor;
import org.redisson.api.RList;
import org.redisson.api.RedissonClient;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 *
 * @author z2huo
 */
@Component
@RequiredArgsConstructor
public class BucketAddScheduleTask {

    private final RedissonClient redissonClient;

    private static final int LIMIT_COUNT = 5;

    @Scheduled(timeUnit = TimeUnit.SECONDS, fixedDelay = 10, initialDelay = 0)
    public void addToken(String userCode) {
        String key = RedisKeyGenerateUtils.AUTHENTICATION_APP.generateMd5("user-login-bucket", userCode);
        RList<Object> list = redissonClient.getList(key);
        if (list.size() < LIMIT_COUNT) {
            list.add("1");
        }
    }


}
